package com.yc.utilslib.reader.card;

import android.os.Parcel;
import android.os.Parcelable;

import com.yc.utilslib.utils.ByteUtils;

/**
 * 作者：TanTun
 * 时间：2017/2/26
 * 邮箱：32965926@qq.com
 * 描述：用户卡实体类
 */

public class UserCard implements Parcelable {
    private long mFactoryId;
    private UserInfo mUserInfo;
    private ValueInfo mConsumeInfo;
    private ValueInfo mWaterInfo;
    private ExtendedInfo mExtendedInfo;

    /**
     * 余额块信息构造函数
     */
    private UserCard(byte[] factoryId) {
        this.mFactoryId = ByteUtils.byteArrayToLong(factoryId, 0, 4);
        this.mUserInfo = null;
        this.mConsumeInfo = null;
        this.mWaterInfo = null;
    }

    protected UserCard(Parcel in) {
        mFactoryId = in.readLong();
        mUserInfo = in.readParcelable(UserInfo.class.getClassLoader());
        mConsumeInfo = in.readParcelable(ValueInfo.class.getClassLoader());
        mWaterInfo = in.readParcelable(ValueInfo.class.getClassLoader());
        mExtendedInfo = in.readParcelable(ExtendedInfo.class.getClassLoader());
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(mFactoryId);
        dest.writeParcelable(mUserInfo, flags);
        dest.writeParcelable(mConsumeInfo, flags);
        dest.writeParcelable(mWaterInfo, flags);
        dest.writeParcelable(mExtendedInfo, flags);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<UserCard> CREATOR = new Creator<UserCard>() {
        @Override
        public UserCard createFromParcel(Parcel in) {
            return new UserCard(in);
        }

        @Override
        public UserCard[] newArray(int size) {
            return new UserCard[size];
        }
    };

    /**
     * 根据传入的数据构造一个新的实例
     *
     * @param factoryId 物理卡号
     * @param userBlock 用户数据块
     * @param consumeBlock 消费数据块
     * @param waterBlock 水控数据块
     * @return 用户卡
     * @exception java.lang.IllegalArgumentException 异常
     */
    public static UserCard getInstance(byte[] factoryId, byte[] userBlock,
                                       byte[] consumeBlock, byte[] waterBlock) {
        if (userBlock == null || userBlock.length != 16)
            throw new java.lang.IllegalArgumentException("用户块的数据不能为NULL或长度必须为16个字节。");

        UserCard userCard = new UserCard(factoryId);
        userCard.setUserInfo(userBlock);
        if (consumeBlock != null && consumeBlock.length == 16) userCard.setConsumeInfo(consumeBlock);
        if (waterBlock != null && waterBlock.length == 16) userCard.setWaterInfo(waterBlock);
        return userCard;
    }

    /**
     * @return 物理卡号
     */
    public long getFactoryId() {
        return mFactoryId;
    }

    /**
     * @return 用户信息
     */
    public UserInfo getUserInfo() {
        return mUserInfo;
    }

    /**
     * @param blockData 用户信息块数据
     */
    public void setUserInfo(byte[] blockData) {
        this.mUserInfo = UserInfo.getInstance(blockData);
    }

    /**
     * @return 消费扇区
     */
    public ValueInfo getConsumeInfo() {
        return mConsumeInfo;
    }

    /**
     * @param blockData 消费钱包块
     */
    public void setConsumeInfo(byte[] blockData) {
        this.mConsumeInfo = ValueInfo.getInstance(blockData);
    }

    /**
     * @return 水控扇区
     */
    public ValueInfo getWaterInfo() {
        return mWaterInfo;
    }

    /**
     * @param blockData 水控钱包块
     */
    public void setWaterInfo(byte[] blockData) {
        this.mWaterInfo = ValueInfo.getInstance(blockData);
    }

    /**
     * @return 扩展信息
     */
    public ExtendedInfo getExtendedInfo() {
        return mExtendedInfo;
    }

    /**
     * @param data 扩展信息数据
     */
    public void setExtendedInfo(byte[] data) {
        this.mExtendedInfo = ExtendedInfo.getInstance(data);
    }

    /**
     * @return 判断卡信息是否读全
     */
    public boolean check() {
        return (mExtendedInfo != null
                && mUserInfo != null
                && (mConsumeInfo != null
                || (mWaterInfo != null)));
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(200);
        sb.append("物理卡号：").append(mFactoryId);
        if (mUserInfo != null) sb.append(System.getProperty("line.separator"))
                .append(mUserInfo.toString());
        if (mConsumeInfo != null) sb.append(System.getProperty("line.separator"))
                .append(mConsumeInfo.toString());
        if (mWaterInfo != null) sb.append(System.getProperty("line.separator"))
                .append(mWaterInfo.toString());
        if (mExtendedInfo != null) sb.append(System.getProperty("line.separator"))
                .append(mExtendedInfo.toString());
        return sb.toString();
    }
}
